package com.fsnbigdata.web;
import com.fsnbigdata.domain.EndangeredSpecies;
import com.fsnbigdata.enums.ResultEnum;
import com.fsnbigdata.exception.ParamException;
import com.fsnbigdata.util.ReflectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.roo.addon.web.mvc.controller.annotations.ControllerType;
import org.springframework.roo.addon.web.mvc.controller.annotations.RooController;
import org.springframework.roo.addon.web.mvc.controller.annotations.responses.json.RooJSON;
import com.fsnbigdata.service.api.EndangeredSpeciesService;
import io.springlets.web.NotFoundException;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import org.springframework.web.util.UriComponents;
import springfox.documentation.annotations.ApiIgnore;

import java.lang.reflect.Method;

/**
 * = EndangeredSpeciesesItemJsonController
 *
 * TODO Auto-generated class documentation
 *
 */
@RooController(entity = EndangeredSpecies.class, pathPrefix = "/api", type = ControllerType.ITEM)
@RooJSON
@RestController
@RequestMapping(value = "/api/endangeredspecies/{id}", name = "EndangeredSpeciesesItemJsonController", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = "EndangeredSpecieses", description = "濒危生物" )
public class EndangeredSpeciesesItemJsonController {

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    private EndangeredSpeciesService endangeredSpeciesService;

    /**
     * TODO Auto-generated constructor documentation
     *
     * @param endangeredSpeciesService
     */
    @Autowired
    public EndangeredSpeciesesItemJsonController(EndangeredSpeciesService endangeredSpeciesService) {
        this.endangeredSpeciesService = endangeredSpeciesService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return EndangeredSpeciesService
     */
    public EndangeredSpeciesService getEndangeredSpeciesService() {
        return endangeredSpeciesService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param endangeredSpeciesService
     */
    public void setEndangeredSpeciesService(EndangeredSpeciesService endangeredSpeciesService) {
        this.endangeredSpeciesService = endangeredSpeciesService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param id
     * @return EndangeredSpecies
     */
    @ModelAttribute
    public EndangeredSpecies getEndangeredSpecies(@PathVariable("id") Long id) {
        EndangeredSpecies endangeredSpecies = endangeredSpeciesService.findOne(id);
        if (endangeredSpecies == null) {
            throw new NotFoundException(String.format("EndangeredSpecies with identifier '%s' not found", id));
        }
        return endangeredSpecies;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param endangeredSpecies
     * @return ResponseEntity
     */
    @GetMapping(name = "show")
    //@PreAuthorize("hasRole('USER')")
    public ResponseEntity<?> show(@ApiIgnore @ModelAttribute EndangeredSpecies endangeredSpecies) {
        return ResponseEntity.ok(endangeredSpecies);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param endangeredSpecies
     * @return UriComponents
     */
    public static UriComponents showURI(EndangeredSpecies endangeredSpecies) {
        return MvcUriComponentsBuilder.fromMethodCall(MvcUriComponentsBuilder.on(EndangeredSpeciesesItemJsonController.class).show(endangeredSpecies)).buildAndExpand(endangeredSpecies.getId()).encode();
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param storedEndangeredSpecies
     * @param endangeredSpecies
     * @param result
     * @return ResponseEntity
     */
    @PutMapping(name = "update")
    @PreAuthorize("hasRole('DB_ES_CS')")
    @ApiOperation(value = "",notes = "update")
    public ResponseEntity<?> update(@ApiIgnore @ModelAttribute EndangeredSpecies storedEndangeredSpecies, @Valid @RequestBody EndangeredSpecies endangeredSpecies, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new ParamException(ResultEnum.PARAM_ERROR.getCode(),
                    result.getFieldError().getDefaultMessage());
        }
        endangeredSpecies.setId(storedEndangeredSpecies.getId());
        endangeredSpecies.getImages().forEach(it -> it.setEndangeredSpecies(endangeredSpecies));
        String name = getEndangeredSpeciesService().findOne(storedEndangeredSpecies.getId()).getName();
        EndangeredSpecies newEndangeredSpecies =  getEndangeredSpeciesService().save(endangeredSpecies);
        Class<?> clazz = EndangeredSpeciesesItemJsonController.class;
        Method method = clazz.getMethod("update", ReflectUtil.getPeremeterClass(clazz, "update"));
        ReflectUtil.setAnnotationOn(method,"更新濒危生物，中文名："+name);

        return ResponseEntity.ok(newEndangeredSpecies);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param endangeredSpecies
     * @return ResponseEntity
     */
    @DeleteMapping(name = "delete")
    @PreAuthorize("hasRole('DB_ES_CS')")
    @ApiOperation(value = "",notes = "delete")
    public ResponseEntity<?> delete(@ApiIgnore @ModelAttribute EndangeredSpecies endangeredSpecies) throws Exception {
        getEndangeredSpeciesService().delete(endangeredSpecies);
        Class<?> clazz = EndangeredSpeciesesItemJsonController.class;
        Method method = clazz.getMethod("delete", ReflectUtil.getPeremeterClass(clazz, "delete"));
        ReflectUtil.setAnnotationOn(method,"删除濒危生物，中文名："+endangeredSpecies.getName());

        return ResponseEntity.ok().build();
    }
}
